
let isDragging = false;

export default function (element, options) {
  const moveFn = function (event) {
    options.drag?.(event);
  };

  const upFn = function (event) {
    document.removeEventListener('mousemove', moveFn, false);
    document.removeEventListener('mouseup', upFn, false);
    document.removeEventListener('touchmove', moveFn, false);
    document.removeEventListener('touchend', upFn, false);

    document.onselectstart = null;
    document.ondragstart = null;

    isDragging = false;

    options.end?.(event);
  };

  const downFn = function (event) {
    if (isDragging) return;
    event.preventDefault();
    document.onselectstart = () => false;
    document.ondragstart = () => false;
    document.addEventListener('mousemove', moveFn, false);
    document.addEventListener('mouseup', upFn, false);
    document.addEventListener('touchmove', moveFn, false);
    document.addEventListener('touchend', upFn, false);

    isDragging = true;

    options.start?.(event);
  };
  element.addEventListener('mousedown', downFn, false);
  element.addEventListener('touchstart', downFn, false);
}
